package pipeline;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangcheng on 2017/4/25 0025.
 */
@Slf4j
public class CapsePipeline extends FilePersistentBase implements Pipeline {

    public CapsePipeline(String path) {
        this.path = path;
    }

    public synchronized void process(ResultItems resultItems, Task task) {
        log.info(String.format("save 51job data,path:%s", path));
        BufferedWriter out = null;
        try {
            String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR + "51job.json";
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true)));
            out.write(JSON.toJSONString(resultItems.getAll()) + "\n");
        } catch (IOException ex) {
            log.warn("write capse file error", ex);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
